import java.awt.Color;

class Orbit1 {

		double dt = 0.02;
		double t = 0;
		double ax;
		double ay;
		double axmid;
		double aymid;
		double xmid;
		double ymid;
		double vxmid;
		double vymid;
		double vx = 0;
		double vy = 2 * Math.PI;
		double x = 1;
		double y = 0;
		double r;
		double rmid;
		double G = 4 * Math.PI * Math.PI;
	
	Orbit1() {
		Plot orbitPlot = new Plot("Orbit of Earth around Sun",-2,2,1,-2,2,1);


		
		//Set up initial plot with sun
		orbitPlot.setPointSize(15);
		orbitPlot.setPointShape(Plot.CIRCLE);
		orbitPlot.setColor(Color.magenta);
		orbitPlot.addPoint(0,0);
		
		//Set plot conditions for earth orbit
		orbitPlot.setPointSize(4);
		orbitPlot.setColor(Color.blue);
		
		//Start Euler algorith and plot path
		System.out.println(totalE());

		accel();

		while (t<50) {
			x += vx*dt+ax*dt*dt/2;
			y += vy*dt+ay*dt*dt/2;
			
			vx += ax*dt/2;
			vy += ay*dt/2;

			r = Math.sqrt(x*x + y*y);		
			
			accel();				

			vx += ax*dt/2;
			vy += ay*dt/2;
			
			orbitPlot.addPoint(x,y);
			
			t += dt;
		}
		System.out.println(totalE());
	}

	public void accel() {
		r = Math.sqrt(x*x + y*y);
		ax = - G * x / (r * r * r);
		ay = - G * y / (r * r * r);	
	}

	double totalE() {
		return -G/Math.sqrt(x*x + y*y) + (vx*vx+vy*vy)/2;	
	}

	
	public static void main(String[] args) {
		new Orbit1();
	}
}
